﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoReception
{
    public class CHotelController
    {
        public static int GetRoomStatList(out List<CHotelRoomCatalog> aRoomList,string aHotelCode)
        {
            int Rst = 0;
            aRoomList = null;
            List<KeyValuePair<Int64, string>> RoomType;
            Rst = CHotelConnect.GetHotelRoomType(out RoomType, aHotelCode);
            if (Rst < 0) return Rst;
            List<CHotelRoom> RoomStatus;
            Rst = CHotelConnect.GetHotelRoomByTypes(out RoomStatus, aHotelCode);
            if (Rst < 0) return Rst;
            List<CHotelGuest> Guests;
            Rst = CHotelConnect.GetRoomNowAndFutureGuest(out Guests, aHotelCode,string.Empty, DateTime.Now);
            if (Rst < 0) return Rst;
            aRoomList = new List<CHotelRoomCatalog>();
            string GuestStatCode;
            CHotelRoomCatalog HotelRooms;
            foreach (KeyValuePair<Int64, string> RoomTypePair in RoomType)
            {
                HotelRooms = new CHotelRoomCatalog();
                HotelRooms.iTypeId = RoomTypePair.Key;
                HotelRooms.iTypeName = RoomTypePair.Value;
                HotelRooms.iRooms = new List<CRoomStat>();
                foreach (CHotelRoom Room in RoomStatus)
                {
                    if (Room.iTypeName == RoomTypePair.Key.ToString("D"))
                    {
                        CRoomStat RoomStat = new CRoomStat();
                        RoomStat.iLocId = Room.iRoomId;
                        RoomStat.iLocation = Room.iRoomNo.Trim();
                        RoomStat.iRoomStatCode = Room.iStatusCode;
                        GuestStatCode = CGuestStatusCode.KEmpty;
                        foreach (CHotelGuest Gst in Guests)
                        {
                            if (Gst.iRoomId == RoomStat.iLocId)
                            {
                                if (Gst.iCheckOutDate.Date > DateTime.Today && Gst.iCheckFlag == CHotelCheckFlag.KCheckIn)
                                {
                                    GuestStatCode = CGuestStatusCode.KSettle;
                                }
                                if (GuestStatCode != CGuestStatusCode.KSettle)
                                {
                                    if (Gst.iCheckFlag != CHotelCheckFlag.KCheckIn)
                                    {
                                        GuestStatCode = CGuestStatusCode.KToArrive;
                                    }
                                    else if (Gst.iCheckOutDate.Date <= DateTime.Today)
                                    {
                                        GuestStatCode = CGuestStatusCode.KToLeave;
                                    }
                                }
                            }
                        }
                        RoomStat.iGuestStatCode = GuestStatCode;
                        HotelRooms.iRooms.Add(RoomStat);
                    }
                }
                aRoomList.Add(HotelRooms);
            }
            return Rst;
        }

        public static int GetHotelList(out List<KeyValuePair<string,string>> aHotels)
        {
            int Rst = 0;
            Rst = CHotelConnect.GetHotels(out aHotels);
            return Rst;
        }

        public static int GetHotelRoomType(out List<KeyValuePair<Int64, string>> aRoomType, string aHotelCode)
        {
            int Rst = 0;
            Rst = CHotelConnect.GetHotelRoomType(out aRoomType, aHotelCode);
            return Rst;
        }

        public static int NewHotel(string aHotelCode,string aHotelName)
        {
            int Rst = 0;
            Rst = CHotelConnect.NewHotel(aHotelCode,aHotelName,"");
            return Rst;
        }

        public static int SetHotelRoomType(string aHotelCode, Int64 aTypeId, string aTypeName)
        {
            int Rst = 0;Int64 NewId;
            Rst = CHotelConnect.SetHotelRoomType(out NewId, aHotelCode, aTypeId,  aTypeName);
            return Rst;
        }

        public static int NewHotelRoom(string aHotelCode,Int64 aTypeId, string aRoomName)
        {
            int Rst = 0; Int64 aRoomId;
            Rst = CHotelConnect.NewHotelRoom(out aRoomId, aHotelCode, aTypeId, aRoomName);
            return Rst;
        }

        public static int UpdateRoomStat(string aHotelCode,Int64 aRoomId,string aOperation)
        {
            List<KeyValuePair<string, int>> UpdateList = new List<KeyValuePair<string, int>>();
            switch(aOperation)
            {
                case CRoomOperation.KClean:
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 0)); break;
                case CRoomOperation.KDirty:
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 1)); break;
                case CRoomOperation.KUnKnowLock:
                    UpdateList.Add(new KeyValuePair<string, int>("except", 1));
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 2)); break;
                case CRoomOperation.KCleanLock:
                    UpdateList.Add(new KeyValuePair<string, int>("except", 1));
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 0)); break;
                case CRoomOperation.KDirtyLock:
                    UpdateList.Add(new KeyValuePair<string, int>("except", 1));
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 1)); break;
                case CRoomOperation.KUnLock:
                    UpdateList.Add(new KeyValuePair<string, int>("except", 0)); break;
                case CRoomOperation.KUnLockDirty:
                    UpdateList.Add(new KeyValuePair<string, int>("except", 0));
                    UpdateList.Add(new KeyValuePair<string, int>("dirty", 1)); break;
            }
            int Rst=CHotelConnect.UpdateRoomState(aRoomId, UpdateList);
            return Rst;
        }

        public static int SetDailyDirty(string aHotelCode)
        {
            return CHotelConnect.SetDailyDirty(aHotelCode);
        }

        public static int CheckInAndOut(string aHotelCode, Int64 aRoomId, string aOperation)
        {
            int Rst = 0;
            List<KeyValuePair<string, int>> UpdateList = new List<KeyValuePair<string, int>>();
            switch (aOperation)
            {
                case CRoomOperation.KCheckIn:
                    UpdateList.Add(new KeyValuePair<string, int>("guest", 1));
                    Rst = CHotelConnect.UpdateRoomState(aRoomId, UpdateList);
                    Rst += CHotelConnect.CheckInAndOutGuest(aRoomId, true);
                    break;
                case CRoomOperation.KCheckOut:
                    UpdateList.Add(new KeyValuePair<string, int>("guest", 0));
                    Rst = CHotelConnect.UpdateRoomState(aRoomId, UpdateList);
                    Rst += CHotelConnect.CheckInAndOutGuest(aRoomId, false);
                    break;
            }
            return Rst;
        }

        public static int GetHotelGuestList(out List<EmSelectItem> aGuestList,string aHotelCode)
        {
            int Rst = 0;
            aGuestList = new List<EmSelectItem>();
            List<EmGuestLite> Guests;
            Rst = CHotelConnect.GetAllGuestList(out Guests, aHotelCode);
            if(Rst>=0)
            {
                foreach(EmGuestLite g in Guests)
                {
                    aGuestList.Add(new EmSelectItem()
                    {
                        iValue = g.iGuestNo,
                        iText = string.Format("{0}[{1}]-vip{2}", g.iGuestName, GetSexName(g.iSex), g.iVip),
                        iTagValue= g
                    });
                }
            }
            return Rst;
        }

        public static int GetBookingGuestsByRoomId(out List<CHotelGuest> aGuestList, string aCode, Int64 aRoomId)
        {
            long BookDate = Lmm.LmmCommonFunction.DateTimeToUTCLong(DateTime.Today);
            BookDate = 0;
            int Rst=CHotelConnect.GetBookingGuestsByRoomId(out aGuestList, aCode, aRoomId,BookDate);
            return Rst;
        }

        public static int GetNewGuestNameByTeam(out string aGstName, string aCode, string aTeamName, int aIndex)
        {
            return CHotelConnect.GetNewGuestNameByTeam(out aGstName, aCode, aTeamName, aIndex);
        }

        public static int CancelBooking(Int64 aRoomId, List<KeyValuePair<string, Int64>> aBookingGuests)
        {
            return CHotelConnect.CancelBooking(aRoomId, aBookingGuests);
        }

        public static int GetHotelAvailableRoom(out List<KeyValuePair<Int64,string>> aRoomList,string aHotelCode,Int64 aTypeId,DateTime aDateFr,DateTime aDateTo)
        {
            int Rst = 0;
            aRoomList = new List<KeyValuePair<long, string>>();
            List<CHotelRoom> RoomStatus;
            Rst = CHotelConnect.GetAvailableRoom(out RoomStatus, aHotelCode,aTypeId, aDateFr, aDateTo);
            if (Rst < 0) return Rst;
            foreach(CHotelRoom room in RoomStatus)
            {
                aRoomList.Add(new KeyValuePair<long, string>(room.iRoomId,room.iRoomNo));
            }
            return Rst;
        }

        private static string GetSexName(string aSexCode)
        {
            string Name = "";
            switch(aSexCode)
            {
                case "M": Name = "男"; break;
                case "F": Name = "女"; break;
                default:Name = "未明";break;
            }
            return Name;
        }

        public static int GetHotelTeams(out List<string> aTeamNames,string aHotelCode)
        {
            int Rst = CHotelConnect.GetAllTeamName(out aTeamNames, aHotelCode);
            return Rst;
        }

        public static int NewHotelGuest(out string aGuestNo, string aCode, EmGuestInfo aGuest)
        {
            int Rst = CHotelConnect.NewGuestInfo(out aGuestNo, aCode, aGuest);
            return Rst;
        }

        public static int NewComingGuest(string aCode, List<EmRoomGuest> aGuestList)
        {
            int Rst = CHotelConnect.NewComingGuest(aCode, aGuestList);
            return Rst;
        }

        public static int RoomOverStay(string aCode, Int64 aRoomId,DateTime aLeaveTime)
        {
            int Rst = CHotelConnect.GuestDelay(aRoomId, aLeaveTime);
            return Rst;
        }
    }
}
